import request from '@/utils/request'
import type { QueryParams, ScheduleQuery, ScheduleSubmit, Counselor, BatchScheduleSubmit } from '@/types/counseling'
import type { ApiResponse, PageResult } from '@/types/common'
import type { Schedule } from '@/types/counseling'
import type { Appointment } from '@/types/counseling'

// 预约相关接口
export function getAppointmentList(params: QueryParams) {
  console.log('发送请求参数:', params)  // 添加日志
  return request<PageResult<Appointment>>({
    url: '/counseling/appointment/list',
    method: 'get',
    params,
    // 添加错误处理
    validateStatus: (status) => {
      return status >= 200 && status < 300
    }
  })
}

export function createAppointment(data: any): Promise<ApiResponse<void>> {
  return request({
    url: '/counseling/appointment/create',
    method: 'post',
    data
  })
}

export function updateAppointment(data: any) {
  return request({
    url: '/counseling/appointment/update',
    method: 'put',
    data
  })
}

// 咨询记录相关接口
export function getRecordList(params: any) {
  return request({
    url: '/record/list',
    method: 'get',
    params
  })
}

export function createRecord(data: any) {
  return request({
    url: '/record/create',
    method: 'post',
    data
  })
}

// 更新咨询记录
export function updateRecord(data: any) {
  return request({
    url: '/api/record/update',
    method: 'put',
    data
  })
}

// 咨询师相关接口
export function getCounselorList(params?: any): Promise<ApiResponse<PageResult<Counselor>>> {
  return request({
    url: '/counselor/list',
    method: 'get',
    params
  })
}

// 排班相关接口
export function getScheduleList(query: ScheduleQuery): Promise<ApiResponse<Schedule[]>> {
  return request({
    url: '/counselor/schedule/list',
    method: 'get',
    params: query
  })
}

export function updateSchedule(data: ScheduleSubmit) {
  return request({
    url: '/counselor/schedule/update',
    method: 'put',
    data
  })
}

// 更新预约状态
export function updateAppointmentStatus(data: any) {
  return request({
    url: '/counseling/appointment/status',
    method: 'put',
    data
  })
}

// 确认预约
export function confirmAppointment(id: number) {
  return request({
    url: `/counseling/appointment/confirm/${id}`,
    method: 'put'
  })
}

// 取消预约
export function cancelAppointment(id: number, reason: string) {
  return request({
    url: `/counseling/appointment/cancel/${id}`,
    method: 'put',
    data: { reason }
  })
}

// 撤回取消的预约
export function revokeAppointment(id: number) {
  return request({
    url: `/counseling/appointment/revoke/${id}`,
    method: 'put'
  })
}

// 添加排班
export function addSchedule(data: ScheduleSubmit): Promise<ApiResponse<void>> {
  return request({
    url: '/counselor/schedule/add',
    method: 'post',
    data
  })
}

// 批量添加排班
export function batchAddSchedule(data: BatchScheduleSubmit): Promise<ApiResponse<void>> {
  return request({
    url: '/counselor/schedule/batchAdd',
    method: 'post',
    data
  })
}

// 获取指定日期范围内的预约记录
export function getAppointmentsByDateRange(params: {
  counselorId?: number | string
  startDate: string
  endDate: string
}) {
  return request({
    url: '/counseling/appointment/list',
    method: 'get',
    params: {
      pageNum: 1,
      pageSize: 100,
      counselorId: params.counselorId,
      startTime: `${params.startDate} 00:00:00`,
      endTime: `${params.endDate} 23:59:59`,
      status: 'CONFIRMED'  // 只查询已确认的预约
    }
  })
}

// 获取可预约时间段
export function getAvailableTimeSlots(params: { counselorId: number | string, date: string }): Promise<ApiResponse<any[]>> {
  return request({
    url: '/counselor/schedule/available-slots',
    method: 'get',
    params
  })
}

// 更新咨询师信息
export const updateCounselor = (id: number, data: Partial<Counselor>) => {
  // 只保留需要更新的字段
  const { 
    realName,
    title,
    specialty,
    introduction,
    workYears,
    qualification,
    certificateUrl
  } = data

  return request({
    url: `/counselor/${id}`,
    method: 'put',
    data: {
      realName,
      title,
      specialty: Array.isArray(specialty) ? specialty.join(',') : specialty, // 将数组转换为逗号分隔的字符串
      introduction,
      workYears,
      qualification,
      certificateUrl
    }
  })
} 